package com.group3.admin.listener;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.command.ActiveMQQueue;

import com.group3.admin.service.IAMerchantService;
import com.group3.admin.service.IValidateResubmitService;
import com.group3.admin.service.impl.AMerchantServiceImpl;
import com.group3.admin.service.impl.ValidateResubmitServiceImpl;
import com.group3.admin.util.JmsUtil;
import com.group3.admin.util.JsonUtil;
import com.group3.entity.AMerchant;

/**
 * Application Lifecycle Listener implementation class ContextListener
 * 
 */
public class ContextListener implements ServletContextListener {
	private IAMerchantService service = new AMerchantServiceImpl();
	private IValidateResubmitService vService = new ValidateResubmitServiceImpl();
	Session session = null;
	MessageConsumer consumer =null;
	Connection conn =null;
	
	public static Date newestDate = new Date();		// 最新M端更新时间
	/**
	 * Default constructor.
	 */
	public ContextListener() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent arg0) {		
			
		new Thread(new Runnable() {
		
			@Override
			public void run() {
				conn = JmsUtil.getConnection();
				if (conn != null) {
					try {
						session = conn.createSession(false,
								Session.AUTO_ACKNOWLEDGE);
						Destination queue = new ActiveMQQueue("Admin-Merchant53");
						consumer= session.createConsumer(queue);
						conn.start();
						consumer.setMessageListener(new MessageListener() {

							public void onMessage(Message msg) {
								TextMessage tm = (TextMessage) msg;
								String readText;
								try {
									readText = tm.getText();
									AMerchant aMerchant2=null;
								System.out.println("JMS GET THE MSG IS "+readText.substring(0, 50));
								AMerchant aMerchant=JsonUtil.getInstance().getObjectFromJson(readText, AMerchant.class);
								
								aMerchant.setType("ASSESSING");
//								System.out.println("parsedJson to amerchant is " + aMerchant);
//								AMerchant aMerchant = mapper.readValue(readText, AMerchant.class);
								boolean validate = vService.validateResubmit(aMerchant.getmId());
								if(validate){
									 aMerchant2= service.updateResubmitAMerchant(aMerchant);
								} else {
									aMerchant2 = service.addMerchant(aMerchant);
								}
								if(aMerchant2!=null){
									newestDate = new Date();
									System.out.println("ContextListener : newest Date updated!!" + newestDate.getTime());
									System.out.println("add or update  the register request in db success");
								}
								else {
									System.out.println("aMerchant2 is null" );
								}
								} catch (JMSException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
						System.out.println("init JMS...");
					} catch (JMSException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		}).start();
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		JmsUtil.free(conn, session, consumer);
	}

}
